//
//  MCAddTicketViewController.swift
//  StoreManage
//
//  Created by MC on 2018/1/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCAddTicketViewController: MCViewController {

    var isUpdate = false   // 是更新优惠券还是新增优惠券
    var promotionDetailId = 0
    
    private var tickerType : Int = 0      // 券的类型 2通用 3品牌 5商品
    private var goodsOrBrandId : Int = 0  // 当券类型是品牌或者商品的时候，记录其id
    private var goodsOrBrandName : String = ""  // 当券类型是品牌或者商品的时候，记录其Name

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        if isUpdate {
            sendNetworking()
        }
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func saveButtonClicked() {
        
        if nameView.textField.text?.count == 0 {
            MCHUD_text(message: "请填写优惠券的名称", view: self.view, delay: 1.2)
            return
        }
        
        if limitView.textField.text?.count == 0 {
            MCHUD_text(message: "请填写满多少可用", view: self.view, delay: 1.2)
            return
        }

        if typeView.contentButton.isSelected == false {
            MCHUD_text(message: "请选择券类型", view: self.view, delay: 1.2)
            return
        }

        if wayView.contentButton.isSelected == false {
            MCHUD_text(message: "请选择优惠方式", view: self.view, delay: 1.2)
            return
        }

        if reduceView.textField.text?.count == 0 {
            MCHUD_text(message: "请填写优惠金额", view: self.view, delay: 1.2)
            return
        }

        if beginTimeView.contentButton.isSelected == false {
            MCHUD_text(message: "请选择生效时间", view: self.view, delay: 1.2)
            return
        }
        
        if endTimeView.contentButton.isSelected == false {
            MCHUD_text(message: "请选择失效时间", view: self.view, delay: 1.2)
            return
        }
        
        if numberView.textField.text?.count == 0 {
            MCHUD_text(message: "请填写总张数", view: self.view, delay: 1.2)
            return
        }
        
        if isMarketShowView.contentButton.isSelected == false {
            MCHUD_text(message: "请选择是否显示在微商城", view: self.view, delay: 1.2)
            return
        }
        
        
        // 失效时间大于生效时间
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date1 = formatter.date(from: beginTimeView.contentButton.currentTitle!)
        let date2 = formatter.date(from: endTimeView.contentButton.currentTitle!)
        let dateCurrent = Date.init()
        
        
        let result : ComparisonResult = date1!.compare(date2!)
        if result == ComparisonResult.orderedDescending {
            MCHUD_text(message: "失效时间应该大于生效时间", view: self.view, delay: 1.2)
            return
        }
        
        
        let result1 : ComparisonResult = dateCurrent.compare(date2!)
        if result1 == ComparisonResult.orderedDescending {
            MCHUD_text(message: "失效时间应该大于当前时间", view: self.view, delay: 1.2)
            return
        }
        
        
        let promotionDetailName = nameView.textField.text
        let limit = Double(limitView.textField.text!)! * 100.0

        
        var isDiscount : Int = 0
        var amount : Double = 0
        var discount : Double = 0
        if wayView.contentButton.currentTitle == "满减" {
            isDiscount = 0
            amount = Double(reduceView.textField.text!)! * 100.0
            discount = 100
        } else {  // 折扣
            isDiscount = 1
            discount = Double(reduceView.textField.text!)! 
        }

        let effectiveTime = beginTimeView.contentButton.currentTitle
        let expirationTime = endTimeView.contentButton.currentTitle
        
        let totalNumber = numberView.textField.text
        
        var forMallDisplay : Int = 0
        if isMarketShowView.contentButton.currentTitle == "是" {
            forMallDisplay = 1
        } else {
            forMallDisplay = 0
        }
     
        
        var urlStr : String = ""
        if isUpdate {
            urlStr = JAVA_updatePromotion
        } else {
            urlStr = JAVA_insertStorePromotion
        }
        
        let para = [
            "promotionDetailName" : promotionDetailName ?? "",
            "limited"             : limit,
            "type"                : self.tickerType,
            "isDiscount"          : isDiscount,
            "amount"              : amount,
            "discount"            : discount,
            "effectiveTime"       : effectiveTime ?? "",
            "expirationTime"      : expirationTime ?? "",
            "totalNumber"         : totalNumber ?? "",
            "forMallDisplay"      : forMallDisplay,
            "promotionDetailId"   : promotionDetailId,
            "mallStoreId"         : MCUserinfo_getStoreId(),
            "goodId"              : goodsOrBrandId,
            "brandId"             : goodsOrBrandId,
            ] as [String : Any]
        
        MCNetwork_post(url: urlStr, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            
            self.navigationController?.popViewController(animated: true)
        }, dislike: { (response) in }) { (error) in }
    }
    
    @objc func keyboardConfirmClicked() {
        self.view.endEditing(true)
    }
    
    @objc func showPickerViewEvent(sender:UIButton) {
        self.view.endEditing(true)
        switch sender.tag {
        case 1001: //券类型
//            createPicerView(type: sender.tag, array: ["品牌券","商品券","通用券"])
            createPicerView(type: sender.tag, array: ["商品券","通用券"])

        case 1002: // 选择品牌或者商品
    
            let vc = MCAddTicketChooseGoodsOrBrandViewController()
            vc.type = tickerType
            vc.delegate = self
            MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
        case 1003: //优惠方式
            createPicerView(type: sender.tag, array: ["满减","折扣"])
        case 1004: //生效时间
            createDatePicerView(type: sender.tag)
        case 1005: //失效时间
            createDatePicerView(type: sender.tag)
        case 1006: //是否显示在微商城
            createPicerView(type: sender.tag, array: ["是","否"])
        default:
            break
        }
    }
    
    func createPicerView(type:Int,array:NSArray) {
        let pickerView = MCSinglePickerView()
        pickerView.delegate = self
        pickerView.dataArray = array
        pickerView.type = type
        view.window!.addSubview(pickerView)
        pickerView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view.window!)
        }
    }
    
    func createDatePicerView(type:Int) {
        let pickerView = MCDatePickerView()
        pickerView.delegate = self
        pickerView.type = type
        pickerView.datePickerView.minimumDate = Date.init()
        view.window!.addSubview(pickerView)
        pickerView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view.window!)
        }
    }
    
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "promotionDetailId" : promotionDetailId,
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_storePromotionDetail, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
            let dataDict = response["data"] as! [String : AnyObject]
            
            let promotionDetailName = dataDict["promotionDetailName"] as? String ?? ""  // 券的名称
            let limited = dataDict["limited"] as? Int ?? 0    // 满多少可用
            self.tickerType = dataDict["type"] as? Int ?? 0          // 2通用券3品牌券5商品券
            let isDiscount = dataDict["isDiscount"] as? Int ?? 0  // 0表示是减金额，1表示是折扣
            let amount = dataDict["amount"] as? Int ?? 0
            let effectiveTime = dataDict["effectiveTime"] as? String ?? ""
            let expirationTime = dataDict["expirationTime"] as? String ?? ""
            let totalNumber = dataDict["totalNumber"] as? Int ?? 0
            let forMallDisplay = dataDict["forMallDisplay"] as? Int ?? 0  // 1在微商城显示0不显示
            let brandId = dataDict["brandId"] as? Int ?? 0
            let brandName = dataDict["brandName"] as? String ?? ""
            let goodId = dataDict["goodId"] as? Int ?? 0
            let goodsName = dataDict["goodsName"] as? String ?? ""


            
            self.nameView.textField.text = promotionDetailName
            self.limitView.textField.text = String(Double(limited) / 100.0)
           
            
            // 类型
            self.typeView.contentButton.isSelected = true
            self.goodsOrBrandsIdView.contentButton.isSelected = true
            var goodsOrBrandsIdView_H : CGFloat = 0
            
            if self.tickerType == 2 {
                self.typeView.contentButton.setTitle("通用券", for: UIControlState.normal)
                self.goodsOrBrandsIdView.isHidden = true
                goodsOrBrandsIdView_H = 0
            } else if self.tickerType == 3 {
                self.typeView.contentButton.setTitle("品牌券", for: UIControlState.normal)
                goodsOrBrandsIdView_H = 44
                self.goodsOrBrandsIdView.isHidden = false
                self.goodsOrBrandsIdView.titleLabel.text = "请选择品牌"
                
                self.goodsOrBrandId = brandId
                self.goodsOrBrandsIdView.contentButton.setTitle(brandName, for: UIControlState.normal)
            } else {
                goodsOrBrandsIdView_H = 44
                self.typeView.contentButton.setTitle("商品券", for: UIControlState.normal)
                self.goodsOrBrandsIdView.isHidden = false
                self.goodsOrBrandsIdView.titleLabel.text = "请选择商品"
                
                self.goodsOrBrandId = goodId
                self.goodsOrBrandsIdView.contentButton.setTitle(goodsName, for: UIControlState.normal)

            }
            self.goodsOrBrandsIdView.snp.remakeConstraints { (make) ->Void in
                make.left.equalTo(self.bgScrollView)
                make.size.equalTo(CGSize.init(width: MCScreenWidth, height: goodsOrBrandsIdView_H))
                make.top.equalTo(self.typeView.snp.bottom).offset(0)
            }

            
            
            // 优惠方式
            self.wayView.contentButton.isSelected = true
            if isDiscount == 0 {
                self.wayView.contentButton.setTitle("满减", for: UIControlState.normal)
                self.reduceView.titleLabel.text = "减金额(元)"
            } else {
                self.wayView.contentButton.setTitle("折扣", for: UIControlState.normal)
                self.reduceView.titleLabel.text = "折扣(%)"
            }
            
            
            
            // 减金额
            self.reduceView.textField.text = String(Double(amount)/100.0)
            
            // 生效和失效时间
            self.beginTimeView.contentButton.isSelected = true
            self.endTimeView.contentButton.isSelected = true
            self.beginTimeView.contentButton.setTitle(effectiveTime.MCString_prefix(index: 19), for: UIControlState.normal)
            self.endTimeView.contentButton.setTitle(expirationTime.MCString_prefix(index: 19), for: UIControlState.normal)

            // 总张数
            self.numberView.textField.text = String(totalNumber)
            
            // 是否显示在微商城
            self.isMarketShowView.contentButton.isSelected = true
            if forMallDisplay == 0 {
                self.isMarketShowView.contentButton.setTitle("否", for: UIControlState.normal)
            } else {
                self.isMarketShowView.contentButton.setTitle("是", for: UIControlState.normal)
            }
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "编辑优惠券"
        self.view.backgroundColor = UIColor.MCGray_light
    }
    
    func initUI() {
        view.addSubview(bgScrollView)
        bgScrollView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
        
        bgScrollView.addSubview(nameView)
        nameView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(bgScrollView)
        }
        
        bgScrollView.addSubview(limitView)
        limitView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(nameView.snp.bottom)
        }
        
        bgScrollView.addSubview(typeView)
        typeView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(limitView.snp.bottom).offset(5)
        }
        
        bgScrollView.addSubview(goodsOrBrandsIdView)
        goodsOrBrandsIdView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 0))
            make.top.equalTo(typeView.snp.bottom).offset(0)
        }

        bgScrollView.addSubview(wayView)
        wayView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(goodsOrBrandsIdView.snp.bottom).offset(5)
        }

        bgScrollView.addSubview(reduceView)
        reduceView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(wayView.snp.bottom)
        }
        
        
        bgScrollView.addSubview(beginTimeView)
        beginTimeView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(reduceView.snp.bottom).offset(5)
        }
        
        
        bgScrollView.addSubview(endTimeView)
        endTimeView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(beginTimeView.snp.bottom)
        }
        
        bgScrollView.addSubview(numberView)
        numberView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(endTimeView.snp.bottom)
        }
        
        bgScrollView.addSubview(isMarketShowView)
        isMarketShowView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgScrollView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
            make.top.equalTo(numberView.snp.bottom).offset(5)
        }

        bgScrollView.addSubview(saveButton)
        saveButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.top.equalTo(isMarketShowView.snp.bottom).offset(40)
            make.size.equalTo(CGSize.init(width: MCScreenWidth - 40, height: 36))
        }
        
        bgScrollView.contentSize = CGSize.init(width: MCScreenWidth, height: CGFloat(10 * 44 + 4 * 5 + 40 + 36 + 20))
    }
    
    func MCAddToolBar(textField:UITextField) {
        let bar = UIToolbar.init(frame: CGRect.init(x: 0, y: 0, width: MCScreenWidth, height: 44))
        let button = UIButton.init(frame: CGRect.init(x: MCScreenWidth-80, y: 7, width: 70, height: 30))
        button.layer.cornerRadius = 5
        button.layer.masksToBounds = true
        button.layer.borderColor = UIColor.MCMain.cgColor
        button.layer.borderWidth = 1
        button.setTitle("确认", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCMain, for: UIControlState.normal)
        button.titleLabel?.font = MCFont(14)
        button.addTarget(self, action: #selector(keyboardConfirmClicked), for: UIControlEvents.touchUpInside)
        bar.addSubview(button)
        bar.layoutIfNeeded()
        bar.sendSubview(toBack: bar.subviews.last!)
        textField.inputAccessoryView = bar
    }
    
    // MARK: - Setter & Getter
    lazy var bgScrollView: UIScrollView = {
        let view = UIScrollView()
        return view
    }()
    
    lazy var nameView: MCAddTicketInputView = {
        let view = MCAddTicketInputView()
        view.titleLabel.text = "优惠券名称"
        view.textField.placeholder = "请输入优惠券的名称"
        view.textField.returnKeyType = UIReturnKeyType.done
        view.textField.delegate = self
        return view
    }()
 
    lazy var limitView: MCAddTicketInputView = {
        let view = MCAddTicketInputView()
        view.titleLabel.text = "满多少可用(元)"
        view.textField.placeholder = "请输入金额"
        view.textField.keyboardType = UIKeyboardType.decimalPad
        view.textField.delegate = self
        MCAddToolBar(textField: view.textField)
        return view
    }()

    lazy var reduceView: MCAddTicketInputView = {
        let view = MCAddTicketInputView()
        view.titleLabel.text = "减金额(元)"
        view.textField.placeholder = "请输入金额/折扣"
        view.textField.keyboardType = UIKeyboardType.decimalPad
        view.textField.delegate = self
        MCAddToolBar(textField: view.textField)
        return view
    }()

    lazy var numberView: MCAddTicketInputView = {
        let view = MCAddTicketInputView()
        view.titleLabel.text = "总张数(张)"
        view.textField.placeholder = "请输入张数"
        view.textField.keyboardType = UIKeyboardType.numberPad
        MCAddToolBar(textField: view.textField)
        return view
    }()

    
    lazy var typeView: MCAddTicketEventView = {
        let view = MCAddTicketEventView()
        view.titleLabel.text = "券类型"
        view.contentButton.setTitle("请选择券类型", for: UIControlState.normal)
        view.contentButton.tag = 1001
        view.contentButton.addTarget(self, action: #selector(showPickerViewEvent(sender:)), for: UIControlEvents.touchUpInside)
        return view
    }()
    
    
    lazy var goodsOrBrandsIdView: MCAddTicketEventView = {
        let view = MCAddTicketEventView()
        view.titleLabel.text = "请选择品牌/商品"
        view.contentButton.setTitle("请选择", for: UIControlState.normal)
        view.contentButton.tag = 1002
        view.isHidden = true
        view.contentButton.addTarget(self, action: #selector(showPickerViewEvent(sender:)), for: UIControlEvents.touchUpInside)
        return view
    }()

    

    lazy var wayView: MCAddTicketEventView = {
        let view = MCAddTicketEventView()
        view.titleLabel.text = "优惠方式"
        view.contentButton.setTitle("请选择优惠方式", for: UIControlState.normal)
        view.contentButton.tag = 1003
        view.contentButton.addTarget(self, action: #selector(showPickerViewEvent(sender:)), for: UIControlEvents.touchUpInside)
        return view
    }()

    lazy var beginTimeView: MCAddTicketEventView = {
        let view = MCAddTicketEventView()
        view.titleLabel.text = "生效时间"
        view.contentButton.setTitle("请选择生效时间", for: UIControlState.normal)
        view.contentButton.tag = 1004
        view.contentButton.addTarget(self, action: #selector(showPickerViewEvent(sender:)), for: UIControlEvents.touchUpInside)
        return view
    }()

    lazy var endTimeView: MCAddTicketEventView = {
        let view = MCAddTicketEventView()
        view.titleLabel.text = "失效时间"
        view.contentButton.setTitle("请选择失效时间", for: UIControlState.normal)
        view.contentButton.tag = 1005
        view.contentButton.addTarget(self, action: #selector(showPickerViewEvent(sender:)), for: UIControlEvents.touchUpInside)

        return view
    }()

    lazy var isMarketShowView: MCAddTicketEventView = {
        let view = MCAddTicketEventView()
        view.titleLabel.text = "显示在微商城"
        view.contentButton.setTitle("请选择是否在微商城显示", for: UIControlState.normal)
        view.contentButton.tag = 1006
        view.contentButton.addTarget(self, action: #selector(showPickerViewEvent(sender:)), for: UIControlEvents.touchUpInside)
        return view
    }()
    
    lazy var saveButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 18
        button.backgroundColor = UIColor.MCMain
        button.titleLabel?.font = MCFont(14)
        button.setTitle("保存", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(saveButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
}

extension MCAddTicketViewController : MCSinglePickerViewDelegate {
    func MCSinglePickerViewSelectedRow(type: Int, row: Int, str: String) {

        switch type {
        case 1001:
            typeView.contentButton.isSelected = true
            
            typeView.contentButton.setTitle(str, for: UIControlState.normal)
            
            if typeView.contentButton.currentTitle == "通用券" {
                goodsOrBrandsIdView.isHidden = true
                self.tickerType = 2
                self.goodsOrBrandsIdView.snp.remakeConstraints { (make) ->Void in
                    make.left.equalTo(self.bgScrollView)
                    make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 0))
                    make.top.equalTo(self.typeView.snp.bottom).offset(0)
                }
            } else if typeView.contentButton.currentTitle == "品牌券" {
                self.tickerType = 3
                goodsOrBrandsIdView.isHidden = false
                self.goodsOrBrandsIdView.titleLabel.text = "请选择品牌"
                self.goodsOrBrandsIdView.contentButton.isSelected = false
                self.goodsOrBrandsIdView.contentButton.setTitle("请选择", for: UIControlState.normal)
                self.goodsOrBrandsIdView.snp.remakeConstraints { (make) ->Void in
                    make.left.equalTo(self.bgScrollView)
                    make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
                    make.top.equalTo(self.typeView.snp.bottom).offset(0)
                }
            } else {  // 商品
                goodsOrBrandsIdView.isHidden = false
                self.tickerType = 5

                self.goodsOrBrandsIdView.titleLabel.text = "请选择商品"
                self.goodsOrBrandsIdView.contentButton.isSelected = false
                self.goodsOrBrandsIdView.contentButton.setTitle("请选择", for: UIControlState.normal)
                self.goodsOrBrandsIdView.snp.remakeConstraints { (make) ->Void in
                    make.left.equalTo(self.bgScrollView)
                    make.size.equalTo(CGSize.init(width: MCScreenWidth, height: 44))
                    make.top.equalTo(self.typeView.snp.bottom).offset(0)
                }
            }
        case 1003:
            wayView.contentButton.isSelected = true
            wayView.contentButton.setTitle(str, for: UIControlState.normal)
            
            reduceView.textField.text = ""
            if str == "满减" {
                self.reduceView.titleLabel.text = "减金额(元)"
                reduceView.textField.placeholder = "请输入金额"
            } else {  // 折扣
                self.reduceView.titleLabel.text = "折扣率(%)"
                reduceView.textField.placeholder = "请输入折扣"
            }
            
        case 1006:
            isMarketShowView.contentButton.isSelected = true
            isMarketShowView.contentButton.setTitle(str, for: UIControlState.normal)
        default:
            break
        }
    }
}
extension MCAddTicketViewController : MCDatePickerViewDelegate {
    func MCDatePickerViewSelectedRow(type: Int, str: String) {
        switch type {
        case 1004:
            beginTimeView.contentButton.isSelected = true
            beginTimeView.contentButton.setTitle(str, for: UIControlState.normal)
        case 1005:
            endTimeView.contentButton.isSelected = true
            endTimeView.contentButton.setTitle(str, for: UIControlState.normal)
        default:
            break
        }
    }
}

extension MCAddTicketViewController : MCAddTicketChooseGoodsOrBrandViewControllerDelegate {
    func MCAddTicketChooseGoodsOrBrandViewControllerChoose(id: Int, name: String) {
        self.goodsOrBrandId = id
        self.goodsOrBrandName = name
        self.goodsOrBrandsIdView.contentButton.setTitle(name, for: UIControlState.normal)
        self.goodsOrBrandsIdView.contentButton.isSelected = true
    }
}

extension MCAddTicketViewController : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if textField == limitView.textField || textField == reduceView.textField  {
            let b = MCString_keepTwoDecimalPlaces(textField, range, string)
            return b
        }
        return true
    }
}

